package com.amigo.online.message.util.onesignal;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.message.mapper.online.UserOneSignalMapper;
import com.amigo.online.message.mybatisbean.UserOneSignal;
import com.amigo.online.message.mybatisbean.UserOneSignalExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class OneSignalUtil{

    @Autowired
    UserOneSignalMapper userOneSignalMapper;

    /**
     *
     * @param headings 标题
     * @param subtitle 副标题
     * @param contents  消息
     * @param userIds 用户ID
     */
    public static void sendByPlayerId(JSONObject headings,JSONObject subtitle, JSONObject contents, String[] userIds){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("app_id",OneSignalHttpUtil.ONESIGNAL_APP_ID);
        jsonObject.put("include_player_ids",userIds);
        jsonObject.put("contents",contents);

        jsonObject.put("headings",headings);
        jsonObject.put("subtitle",subtitle);

        RestTemplate restTemplate =new RestTemplate();
        log.info("发送通知:" + jsonObject.toJSONString());
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(jsonObject, OneSignalHttpUtil.getHeaders());
        ResponseEntity<String> responseEntity =  restTemplate.exchange(OneSignalURLEnum.CREATE_NOTIFICATION.url, HttpMethod.POST, requestEntity , String.class);
        log.info("发送通知消息返回值: " + responseEntity.getBody());
    }

    /**
     *
     * @param heading
     * @param contents
     * @param userIds
     * @return
     */
    public boolean sendByUserId(JSONObject heading, JSONObject contents, Long[] userIds){
        if(userIds==null){
            return false;
        }
        String[] playerIds = selectUserOneSignalIdByUserId(userIds);
        sendByPlayerId(heading, null, contents, playerIds);
        return true;
    }
    /**
     *
     * @param heading 标题
     * @param contents 消息
     * @param userId
     * @return
     */
    public  boolean sendByUserId(String heading, JSONObject contents, Long[] userId){

        String[] playerIds = selectUserOneSignalIdByUserId(userId);
        if(playerIds==null){
            return false;
        }
        JSONObject headings = new JSONObject();
        headings.put("en", heading);

        sendByPlayerId(headings, null, contents, playerIds);
        return true;
    }
    public  boolean sendByUserId(String heading, String contents, Long... userId){
        JSONObject message = new JSONObject();
        message.put("en", contents);
        sendByUserId(heading,  message, userId);
        return true;
    }



    public  String[] selectUserOneSignalIdByUserId(Long... userId){
        UserOneSignalExample userOneSignalExample = new UserOneSignalExample();
        userOneSignalExample.createCriteria().andUserIdIn(Arrays.asList(userId));
        List<UserOneSignal> list = userOneSignalMapper.selectByExample(userOneSignalExample);
        if(list.size()==0){
            String s= "";
            for (long l :userId){
                s+=l+",";
            }
            log.info("向userId为:" + userId + "的用户发推送,但找不到Signal" );
            return null;
        }
        String[] playerIds = new String[list.size()];
        for (int i=0 ;i<list.size() ;i++){
            playerIds[i] = list.get(i).getOneSignalId();
        }
        return playerIds;
    }


}